package Mystack;

import java.util.Arrays;

public class myStack {
    private int[] elem;
    private int usedsize;

    private  static final int DEFECT_CAPACITY=10;

    public myStack(){
        this.elem=new int[DEFECT_CAPACITY];
    }

    public void push(int val){
        if(isFull(elem)){
            this.elem=Arrays.copyOf(elem,elem.length*2);
        }
        elem[usedsize]=val;
        usedsize++;
    }

    public boolean isFull(int[] elem){
        return this.usedsize==elem.length;
    }

    public int pop(){

        if(usedsize==0){
            throw new EmptyException("栈为空");
        }
        usedsize--;
        return elem[usedsize];
    }

    public int peek(){

        if(usedsize==0){
            throw new EmptyException("栈为空");
        }
        return elem[usedsize-1];
    }

    public boolean isEmpty(){
        return usedsize==0;
    }
}
